import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import get from '@dfeidao/atom-web/local/get';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
// import whoami from '@dfeidao/atom-web/msg/whoami';
import render from '@dfeidao/atom-web/render/render';
// import XL from '@dfeidao/fd-w000018';
import P01 from './P01';
import P02 from './P02';
import P03 from './P03';

export default async function a003(fd: IFeidaoAiBrowserComponent) {
	// 局部刷新
	let page_no = Number(get(fd, 'page_no'));
	if (!page_no) {
		page_no = 1;
	}

	let page_size = Number(get(fd, 'page_size'));
	if (!page_size) {
		page_size = 20;
	}
	// const user = await whoami();
	const productid = get(fd, 'productid') as string;

	// 租户名称
	const name = fd.data.node.querySelector<HTMLInputElement>('[data-feidao-name="name"]').value;
	const q = {} as { tenant_name: object, link_man: object, productid: string, tenant_level: string, is_disable: string };
	q.productid = productid;
	if (name) {
		q.tenant_name = { $regex: '.*' + name + '.*', $options: 'i' };
	}

	// 联系人
	const link_man = fd.data.node.querySelector<HTMLInputElement>('[data-feidao-name="link_man"]').value;
	if (link_man) {
		q.link_man = { $regex: '.*' + link_man + '.*', $options: 'i' };
	}
	// 等级
	// const levl = JSON.parse(JSON.stringify(await (fd.data.node.querySelector('[id="xiala1"]') as XL).get_selected())) as { level_no: string };
	const levl = fd.data.node.querySelector<HTMLSelectElement>('[id="xiala1"]');
	const sel = levl.selectedIndex;
	const value = levl.options[sel].value;
	if (value !== '全部') {
		q.tenant_level = value;
	}

	// 状态
	const status = fd.data.node.querySelector<HTMLSelectElement>('[id="xiala2"]');
	const sel1 = status.selectedIndex;
	const value1 = status.options[sel1].value;
	if (value1 !== '全部') {
		if (value1 === '启用中') {
			q.is_disable = 'false';
		} else {
			q.is_disable = 'true';
		}

	}
	let asc = get(fd, 'asc');
	let desc = get(fd, 'desc');
	if (!asc && !desc) {
		asc = ['tenantid'];
	}
	if (!asc) {
		asc = [];
	}

	if (!desc) {
		desc = [];
	}


	const query = {
		filter: q,
		page_size,
		page_no,
		asc,
		desc
	};

	const res = await nodejs<{ dataArray: Array<{}>, page_no: number, page_size: number }>('tenant-inf/zj-000001/s001', query);

	if (res.dataArray.length === 0) {
		fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="P01"]').classList.add('display-n'); // 数据
		fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="P02"]').classList.remove('display-n'); // 空模板
		fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="P03"]').classList.add('display-n'); // 分页
		// fd.data.node.querySelector<HTMLDivElement>('[data-feidao-name="selpage"]').classList.add('display-n');
		render(fd, '', P02, 'P02', 'inner');
	} else {
		fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="P01"]').classList.remove('display-n');
		fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="P02"]').classList.add('display-n');
		fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="P03"]').classList.remove('display-n');
		// fd.data.node.querySelector<HTMLDivElement>('[data-feidao-name="selpage"]').classList.remove('display-n');
		render(fd, res.dataArray, P01, 'P01', 'inner');
		res.page_no = page_no;
		res.page_size = page_size;
		render(fd, res, P03, 'P03', 'inner');
	}
}
